/**
 * 
 * @Project HRL Simulation
 * @Date 20.06.2011
 * @Author Aebi Pascal, Kocher Marc, Sriranjan George
 * @School BFH-TI Biel, Projekt 1
 *
 */

package utils;

import main.ControlCenter;
import elements.Buffer;
import elements.Lift;


public class StatisticsManager {
	
	private ControlCenter cc = null;
	private String [] tableHeader = new String[5];
	private String [][] tableData = new String[100000][5];
	
	
	public StatisticsManager(ControlCenter cc) {
		this.cc = cc;
		
		tableHeader[0] = "Resource name";
		tableHeader[1] = "Average idle time";
		tableHeader[2] = "Average wait time";
		tableHeader[3] = "Average book time";
		tableHeader[4] = "Number of entries";
	}
	
	
	public String[][] collectData(){
		Agenda a = null;
		int i = 0;
		
		int resourceSize = cc.getBuffers().size()+cc.getCon().getResources().size()+cc.getLifts().size();
		tableData = new String[resourceSize][5];
		
		for (Buffer buffer : cc.getBuffers()) {
			a = buffer.getAgenda();
			tableData[i][0] = "buffer ("+buffer.getPosition()+")";
			tableData[i][1] = String.format("%.3f", a.getAvgIdleTime());
			tableData[i][2] = String.format("%.3f", a.getAvgWaitTime());
			tableData[i][3] = "  -  ";
			tableData[i][4] = Integer.toString(a.getNumberOfEntries());
			i++;
		}
		
		for (Resource resource : cc.getCon().getResources()) {
			a = resource.getAgenda();
			tableData[i][0] = "Conveyor resource ("+resource.getPosition()+")";
			tableData[i][1] = String.format("%.3f", a.getAvgIdleTime());
			tableData[i][2] = "  -  "; //String.format("%20s", String.format("%.3f", a.getAvgWaitTime()));
			tableData[i][3] = "  -  ";
			tableData[i][4] = Integer.toString(a.getNumberOfEntries());
			i++;
		}
		
		for (Lift lift : cc.getLifts()) {
			a = lift.getAgenda();
			tableData[i][0] = "Lift ("+lift.getPosition()+")";
			tableData[i][1] = String.format("%.3f", a.getAvgIdleTime());
			tableData[i][2] = String.format("%.3f", a.getAvgWaitTime());
			tableData[i][3] = String.format("%.3f", a.getAvgBookingTime());
			tableData[i][4] = Integer.toString(a.getNumberOfEntries());
			i++;
		}
		return tableData;
	}
	
	
	public String[] getHeader(){
		return tableHeader;
	}
	
	
	public void printData(){
		//header
		for(int x=0;x<5;x++){
			if(x == 0){
				System.out.print(String.format("%-40s", tableHeader[x]));
			}else{
				System.out.print(String.format("%20s", tableHeader[x]));
			}
		}
		System.out.println();
		
		//data
		for(int i=0;i<tableData.length;i++){
			for(int x=0;x<5;x++){
				if(x == 0){
					System.out.print(String.format("%-40s", tableData[i][x]));
				}else{
					System.out.print(String.format("%20s", tableData[i][x]));
				}
			}
			System.out.println();
		}
	}
}
